package org.docshare.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.docshare.zhuishu.Config;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

public class TextUtilNew {

	private static final String TAG = "TextUtilNew";
	Rect rect;
	private int pos;
	private String text = null;
	char[] chs;
	int w, h;
	int dw = 30;
	int dh = 30;
	Paint textPaint = null;
	public Paint backPaint = null;

	private int textsz;
	private int numPerLine = 15;
	private String title="";
	private Paint borderPaint;

	public TextUtilNew(Rect r) {
		/*
		 * StringBuffer sb = new StringBuffer(); for(int i=0;i<100;i++){
		 * sb.append("测试数据"); } setText(sb.toString(), 0, 30);
		 */
	}

	public void resize(Rect r) {
		if (r != null) {
			rect = r;
			w = rect.right - rect.left;
			h = rect.bottom - rect.top;
		}
		if(numPerLine == 0){
			numPerLine = 15;
		}
		this.textsz = w / numPerLine - 2;
		dw = textsz + 2;
		dh = (int) (textsz * 1.5);
		if (dh <= 0) {
			dh = 30;
		}
		if (dw <= 0) {
			dw = 20;
		}
		textPaint = PaintHelper.getNormalPaint(textsz, Config.read_txt_color);
		titlePaint = PaintHelper.getNormalPaint((int) (textsz*0.8), Config.read_txt_color);
		backPaint = PaintHelper.getDrawPaint(textsz, Config.read_back);
		borderPaint = PaintHelper.getBorderPaint(1, Config.read_txt_color);
	}

	public void setText(String text, int pos, int numPerLine,String title) {
		this.title = title;
		this.numPerLine = numPerLine;
		this.text = text;
		cur_page = 0;
		chs = text.toCharArray();
		Log.i("TextUtilNew", text);
		this.pos = pos;
		;
		resize(null);
		parse();
	}

	ArrayList<Integer> pageStart = new ArrayList<Integer>();
	
	public void parse() {
		pageStart.clear();
		if (dw == 0) {
			return;
		}
		Log.i(TAG, String.format("w=%d,h=%d,dw=%d,dh=%d", w, h, dw, dh));

		int p = 0;
		while(p<text.length()){
			pageStart.add(p);
			p = drawOrParse(null,p);
			if(p== 0)break; //nothing to parse
		}
		
		Log.i(TAG, "page count = " + pageStart.size());
	}

	int cur_page = 0;
	private Paint titlePaint;

	public boolean nextPage() {
		if (cur_page+1 >= pageStart.size()) {
			return false;
		}
		cur_page++;
		pos = pageStart.get(cur_page);
		return true;
	}

	public boolean prevPage() {
		if (cur_page-1 < 0) {
			return false;
		}
		cur_page--;
		pos = pageStart.get(cur_page);
		return true;
	}

	public void draw(Canvas c) {
		drawOrParse(c,pos);
	}
	
/*	public void drawOrParse(Canvas c){
		if (dw == 0) {
			return;
		}

		int p = pos;
		int now_x = rect.left ;
		int now_y = (int) (rect.top + dh*1.8);
		
		if(cur_page == 0 || (p-1>0 && chs[p-1] == '\n')){
			now_x +=  dw * 2;
		}
		
		if (text == null) {
			if(c!=null){
				c.drawText("加载中...", now_x + dw * (numPerLine / 2 - 3),
					(rect.top + rect.bottom) / 2, textPaint);
			}
			return;
		}

		// c.drawRect(rect, backPaint);
		Log.i(TAG, String.format("w=%d,h=%d,dw=%d,dh=%d", w, h, dw, dh));

		boolean lineFirst = true;
		while ( p < chs.length) {
			char ch = chs[p++];
			if (ch == '\n') { //如果遇到换行，则重置now_x,now_y ,如果now_y 到达底部，则换页。
				now_y += (int) (dh * 1.2);
				now_x = rect.left + 2 * dw;
				lineFirst = true;
				if (now_y  >= rect.bottom) {
					if (c==null && p < chs.length) {
						pageStart.add(p);
						now_x = rect.left ;
						now_y = (int) (rect.top + dh*1.8f);
						lineFirst = true;
						if(cur_page == 0 || (p-1>0 && chs[p-1] == '\n')){
							now_x +=  dw * 2;
						}
					} else { // if now is drawing ,break;
						break;
					}
				}
				
				continue;
			}
			if (ch == '\t' || ch == ' ' || ch == '\r')
				continue;
			if (isEnglish(ch))
				continue;
			if (lineFirst && isBiaoDian(ch)){
				continue;
			}

			if(c!=null)c.drawText(ch + "", now_x, now_y, textPaint);
			now_x += dw;
			
			
			if (now_x >= rect.right - dw) {
				now_x = rect.left;
				now_y += dh;
				if (now_y  >= rect.bottom) {
					if (c==null && p < chs.length) {
						pageStart.add(p);
						now_x = rect.left ;
						now_y = (int) (rect.top + dh*1.8f);
						lineFirst = true;
						if(cur_page == 0 || (p-1>0 && chs[p-1] == '\n')){
							now_x +=  dw * 2;
						}
					} else { // if now is drawing ,break;
						break;
					}
				}
				
			}
			lineFirst = false;

			
		}
		if(c!=null){
			c.drawText(title, rect.left, rect.top+dh*0.3f, titlePaint);
			drawBattery(c, rect.left+w*0.9f, rect.top+0.3f*dh - textsz*0.7f , 0.15f);
			drawTime(c, rect.left+w*0.7f, (float) (rect.top+dh*0.3));
			//c.drawRect(rect.left, rect.bottom-5, rect.left+10, rect.bottom+5, borderPaint);
		}
	}*/
	
	public int drawOrParse(Canvas c,int ppp){
		if (dw == 0) {
			return 0;
		}

		int p = ppp;
		int now_x = rect.left ;
		int now_y = (int) (rect.top + dh*1.8);
		
		if(cur_page == 0 || (p-1>0 && chs[p-1] == '\n')){
			now_x +=  dw * 2;
		}
		
		if (text == null) {
			if(c!=null){
				c.drawText("加载中...", now_x + dw * (numPerLine / 2 - 3),
					(rect.top + rect.bottom) / 2, textPaint);
			}
			return 0;
		}
		// c.drawRect(rect, backPaint);
		Log.i(TAG, String.format("w=%d,h=%d,dw=%d,dh=%d", w, h, dw, dh));

		boolean lineFirst = true;
		while ( p < chs.length) {
			char ch = chs[p++];
			if (ch == '\n') { //如果遇到换行，则重置now_x,now_y ,如果now_y 到达底部，则换页。
				now_y += (int) (dh * 1.2);
				now_x = rect.left + 2 * dw;
				lineFirst = true;
				if (now_y  >= rect.bottom) {
						break;
				}
				
				continue;
			}
			if (ch == '\t' || ch == ' ' || ch == '\r')
				continue;
			if (isEnglish(ch))
				continue;
			if (lineFirst && isBiaoDian(ch)){
				continue;
			}

			if(c!=null)c.drawText(ch + "", now_x, now_y, textPaint);
			now_x += dw;
			
			
			if (now_x >= rect.right - dw) {
				now_x = rect.left;
				now_y += dh;
				if (now_y  >= rect.bottom) {
						break;
				}
			}
			lineFirst = false;
		}
		if(c!=null){
			c.drawText(title, rect.left, rect.top+dh*0.3f, titlePaint);
			drawBattery(c, rect.left+w*0.9f, rect.top+0.3f*dh - textsz*0.7f , (float) (textsz*0.6/100));
			drawTime(c, rect.left+w*0.7f, (float) (rect.top+dh*0.3));
			//c.drawRect(rect.left, rect.bottom-5, rect.left+10, rect.bottom+5, borderPaint);
			String page_str = String.format("%d/%d",cur_page+1,pageStart.size());
			c.drawText(page_str, rect.left, rect.bottom+dh*0.2f, titlePaint);
		}
		
		return p;
	}
	
	
	public void drawBattery(Canvas c,float x,float y ,float scale){
		int w = (int) (200*scale);
		int h = (int) (100*scale);
		
		int p = BatteryTool.getIns().getPercent();
		c.drawRect(x, y, x+w, y+h, borderPaint);
		c.drawRect(x, y,x+w*p/100,y+h,textPaint);
		c.drawRect(x+w,y+h*0.3f,x+w+0.4f*h,y+h*0.7f,textPaint);
	}
	public void drawTime(Canvas c,float d,float y){
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		String t = df.format(new Date());
		c.drawText(t, d, y, titlePaint);
	}

	private boolean isBiaoDian(char ch) {
		// TODO Auto-generated method stub
		return (ch == ',') || (ch == ';') || (ch == '。');
	}

	public boolean isEnglish(char ch) {
		ch = Character.toLowerCase(ch);
		return (ch >= 'a') && (ch <= 'z');
	}

}
